//
//  moreBanlirenViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/7.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class moreBanlirenViewController: UIViewController {
    var parentID:String!
    
    var tableView: UITableView!
    
    var buttonTag:NSInteger!
    
    
    var xiangmu:String!
    
    
    var json :String!
    
    var zhiwei :String!
    
    var morebanlirenArray = [moreBanlirenModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "办理人"
        
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - 49), style: .plain )
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorInset = UIEdgeInsets.zero
        //        tableView.separatorStyle = .none
        tableView.backgroundColor = bgGrayColor
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        self.view.addSubview(tableView)
        

        
        
            tableView.register(NSClassFromString("moreBanlirenTableViewCell"), forCellReuseIdentifier: "moreBanlirenTableViewCell")
        
         self.loadData()
    }
    
    func loadData(){
        
        
        let manager = UserDefaults()
        
        
        let token: String? = manager.string(forKey:"tokens")  //根据关键字取值
        

        
        

        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/organization/list"
        
        if self.zhiwei == "办理人" {
            self.json = "{\"target\":\"\("assignee")\",\"token\":\"\(token!)\",\"parentId\":\"\(self.parentID!)\"}"
        }else if self.zhiwei == "审批人"{
            self.json = "{\"target\":\"\("shengpiAssignee")\",\"token\":\"\(token!)\",\"parentId\":\"\(self.parentID!)\"}"
        }else if self.zhiwei == "推送人"{
            self.json = "{\"target\":\"\("pushAssignee")\",\"token\":\"\(token!)\",\"parentId\":\"\(self.parentID!)\"}"
        }else{
            self.json = "{\"target\":\"\("questionAssignee")\",\"token\":\"\(token!)\",\"parentId\":\"\(self.parentID!)\"}"
        }
        
        
//        let json = "{\"token\":\"\(token!)\",\"parentId\":\"\(self.parentID!)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("2222222222json====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                
                print("2222222222json====\(json["message"])")
                let array = json["resultData"].array
                
                if array == nil{
                    return
                }else{
                    for dataDic in  array! {
                        
                        let model1 = moreBanlirenModel  ()
                        model1.id = dataDic["id"].string
                        
                        model1.adminUserPic = dataDic["adminUserPic"].string
                        model1.isParent = dataDic["isParent"].string
                        
                        model1.pId = dataDic["pId"].string
                        
                        model1.name = dataDic["name"].string
                        model1.position = dataDic["position"].string
                        
                        model1.type = dataDic["type"].string
                        
                        self.morebanlirenArray.append(model1)
                        
                        
                        print("NextListArray====\(self.morebanlirenArray.count)")
                        
                    }
                    
                    self.tableView.reloadData()
                }


            }else{

                print("数据请求失败")
            }
            
            
            
            
        }
        
    }
    


}
extension moreBanlirenViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.morebanlirenArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let morebanliren:moreBanlirenModel = self.morebanlirenArray[indexPath.row]
        
       
        
    

        
        if morebanliren.adminUserPic == nil{
        
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            if cell == nil {

                cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
            }

            cell?.textLabel?.text = morebanliren.name
            cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
            cell?.selectionStyle = UITableViewCellSelectionStyle.none
            return cell!

        }else{
        
            let cell = moreBanlirenTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "moreBanlirenTableViewCell")
            
            cell.nameLabel.text = morebanliren.name
            cell.positionLabel.text = morebanliren.position
            
             cell.banlirenImageView.sd_setImage(with: NSURL.init(string: "http://wwwqjzsyzcscom.aykj.net.cn/api/\(morebanliren.adminUserPic!)")! as URL, placeholderImage: UIImage.init(named: "morentouxiang"), options: [], progress: nil, completed:nil)
            
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            
            return cell
        }

       
 
   
    
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
         let morebanliren:moreBanlirenModel = self.morebanlirenArray[indexPath.row]
        
        
        if self.xiangmu == "项目" {
            
            if morebanliren.adminUserPic == nil{
                
                let moreBanlirenVc = moreBanlirenViewController()
                moreBanlirenVc.parentID = morebanliren.id
                moreBanlirenVc.buttonTag = self.buttonTag
                self.navigationController?.pushViewController(moreBanlirenVc , animated: true)
                
            }else{
                NotificationCenter.default.post(name: NSNotification.Name("issTest"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                
                
                let vc  = self.navigationController?.viewControllers[2] as! XiangMuLuRuBanliViewController
                self.navigationController?.popToViewController(vc, animated: true)
            }
            
            
     
           
        }else if  self.xiangmu == "确认"{
            if morebanliren.adminUserPic == nil{
                
                let moreBanlirenVc = moreBanlirenViewController()
                moreBanlirenVc.parentID = morebanliren.id
                moreBanlirenVc.buttonTag = self.buttonTag
                self.navigationController?.pushViewController(moreBanlirenVc , animated: true)
                
            }else{
                
                if self.buttonTag == 100{
                    NotificationCenter.default.post(name: NSNotification.Name("querenluru"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                    
                    
                    let vc  = self.navigationController?.viewControllers[3] as! queRenViewController
                    self.navigationController?.popToViewController(vc, animated: true)
                }else{
                    NotificationCenter.default.post(name: NSNotification.Name("querentuisong"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                    
                    
                    let vc  = self.navigationController?.viewControllers[3] as! queRenViewController
                    self.navigationController?.popToViewController(vc, animated: true)
                }
                
               
            }
            
        }else{
            if morebanliren.adminUserPic == nil{
                
                let moreBanlirenVc = moreBanlirenViewController()
                moreBanlirenVc.parentID = morebanliren.id
                moreBanlirenVc.buttonTag = self.buttonTag
                self.navigationController?.pushViewController(moreBanlirenVc , animated: true)
                
            }else{
                //办理人
                if self.buttonTag == 88{
                    NotificationCenter.default.post(name: NSNotification.Name("isTest"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                    
                    //        [self.navigationController popToRootViewControllerAnimated:YES];
                    //
                    let vc  = self.navigationController?.viewControllers[1] as! LuruViewController
                    self.navigationController?.popToViewController(vc, animated: true)
                }else if self.buttonTag == 99{
                    NotificationCenter.default.post(name: NSNotification.Name("shenpi"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                    
                    //        [self.navigationController popToRootViewControllerAnimated:YES];
                    //
                    let vc  = self.navigationController?.viewControllers[1] as! LuruViewController
                    self.navigationController?.popToViewController(vc, animated: true)
                }else if self.buttonTag == 111{
                    NotificationCenter.default.post(name: NSNotification.Name("tuisong"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                    
                    //        [self.navigationController popToRootViewControllerAnimated:YES];
                    //
                    let vc  = self.navigationController?.viewControllers[1] as! LuruViewController
                    self.navigationController?.popToViewController(vc, animated: true)
                }else{
                    NotificationCenter.default.post(name: NSNotification.Name("wenti"), object: self, userInfo: ["name":morebanliren.name,"photo":morebanliren.adminUserPic,"id":morebanliren.id])
                    
                    //        [self.navigationController popToRootViewControllerAnimated:YES];
                    //
                    let vc  = self.navigationController?.viewControllers[1] as! LuruViewController
                    self.navigationController?.popToViewController(vc, animated: true)
                }
                
                
            }
            
        }
        
        
        
     
        
        
    }
    
}
